package com.activities;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;

import com.example.santiagotur.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class ActivityMap extends FragmentActivity {

	private Double latitud;
	private Double longitud;
	private GoogleMap map;
	private final int mapzoom = 13;
	private LatLng point;

	private void addMark(LatLng point) {

		map.addMarker(new MarkerOptions().position(point).icon(
				BitmapDescriptorFactory.defaultMarker()));

		moveCamera(point);
	}

	private void exit() {

		finish();

	}

	public void moveCamera(LatLng punto) {

		map.animateCamera(CameraUpdateFactory.newLatLngZoom(punto, mapzoom));

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		map = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();

		Bundle extras = getIntent().getExtras();

		if (extras != null) {

			latitud = extras.getDouble("latitud", 0);
			longitud = extras.getDouble("longitud", 0);

			point = new LatLng(latitud, longitud);

			addMark(point);
		}

	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			//
			return true;
		}
		//
		return super.onKeyUp(keyCode, event);
	}

}
